const complaintsDao = require('../../app/facade/complaints.facade.js'),
    merchantDao = require('../../app/facade/merchantInfo.facade.js'),
    _ = require('lodash');

module.exports = {
    receiveComplaints: receiveComplaints,
    update: update,
    getlist : getlist
}

function receiveComplaints(req, res, next){  //cname,reason
    let complaints = req.body;
    if (_.isEmpty(complaints)) {
        res.json('complaints is empty');
    } else {
        merchantDao.getByName(complaints.cname,function(error, data){
            let merchant = data;
            complaints.merchant = merchant;
            complaintsDao.insertOne(complaints, (error, result) => {
                if(error) return next(error,req,res);
                res.json(result);
            });
        })
    }
}

function update(req, res, next){
    let complaints = req.body;
    if (_.isEmpty(complaints)) {
        res.json('complaints is empty');
    } else {
        complaints.complaintsTime = new Date();
        complaintsDao.updateStatus(complaints, (error, result) => {
            if(error) return next(error,req,res);
            res.json(result);
        });
    }
}

function getlist(req, res, next){
    complaintsDao.gets((error, result) => {
        if(error) return next(error,req,res);
        res.json(result);
    });
}